//
//  KuangJiTableViewCell.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/10/23.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
class KuangJiTableViewCell: UITableViewCell {
    @IBOutlet weak var topLabel: UILabel!
    
    @IBOutlet weak var timeLabel2: UILabel!
    
    @IBOutlet weak var grayBackView: UIView!
    
    
    @IBOutlet weak var rightLabel: UILabel!
    @IBOutlet weak var leftLabel: UILabel!
    private var myModel:productModel? = productModel()
    var number : Int?
    var mnType : String?
    var  mytempDic = NSDictionary()
    var typeNumber : Int?
    var isfirst : Int?
    var  myDic :NSDictionary {
        set{
            mytempDic = newValue
            let str = (mytempDic as! Dictionary<String, Any>)["shengyu"] as! String
            
            let tempfloat = ((mytempDic as! Dictionary<String, Any>)["shengyu_time"] as!Float)/((mytempDic as! Dictionary<String, Any>)["all_time"]as! Float)
            
            print("------%f",Float(tempfloat))
//            grayBackView.snp.makeConstraints { (make) in
//                make.left.top.bottom.equalTo(self.topLabel)
//                make.width.equalTo(floorf(Float(kScreenWidth - 30))*tempfloat)
//            }
            topLabel.layer.borderWidth = 1;
            
            
            grayBackView.frame = CGRect(x: 15, y: 10, width: Int(floorf(Float(kScreenWidth - 30))*tempfloat + 1), height: 20)
            
//            grayBackView.frame = CGRect(x: 100, y: 10, width: kScreenWidth - 100, height: 20)
            grayBackView.layer.cornerRadius = 10
            grayBackView.backgroundColor = UIColor("#C1C3C9")
            
            let str2 = str.replacingOccurrences(of: " ", with: XALanguageManager.getString(key: "天"))
            if self.typeNumber == 0{
               
                if myModel?.baozhi_status == "0"{
                    topLabel.layer.borderColor = UIColor("#F952D3")?.cgColor
                    //topLabel.backgroundColor =
                }else{
                   topLabel.layer.borderColor = UIColor("#478EF8")?.cgColor
                    
                }
                if (mytempDic as! Dictionary<String, Any>)["baozhi_status"] as! Int == 1 {
                   topLabel.layer.borderColor = UIColor("#AB46BB")?.cgColor
                    //topLabel.backgroundColor = UIColor("#FA4E5A")
                    
                    if mnType == "2"{
                        timeLabel2.text = String(format: "MT%d  · %@  ( %@ )", self.number! ,(mytempDic as! Dictionary<String, Any>)["n"] as! String,str2)
                    }else{
                        timeLabel2.text = String(format: "CD%d  · %@  ( %@ )", self.number! ,(mytempDic as! Dictionary<String, Any>)["m"] as! String,str2)
                    }
                    

                }else{
                    
                    if mnType == "2"{
                        timeLabel2.text = String(format: "MT%d  ( %@ )", self.number!,str2)
                    }else{
                        timeLabel2.text = String(format: "CD%d  ( %@ )", self.number!,str2)
                    }
                    
                    
                }
                
                if (mytempDic as! Dictionary<String, Any>)["status"] as! Int == 1 {
                   topLabel.layer.borderColor = UIColor("#C1C3C9")?.cgColor
                    
                    
                    grayBackView.frame = CGRect(x: 15, y: 10, width: 0, height: 20)
                    //topLabel.backgroundColor = UIColor("#C1C3C9")
                }
                
                if self.isfirst == 0 {
                    if mnType == "2"{
                        timeLabel2.text = String(format: "MT%d  ( %@ )", self.number! ,str2)
                    }else{
                        timeLabel2.text = String(format: "CD%d  ( %@ )", self.number! ,str2)
                    }
                    
                    //topLabel.backgroundColor = UIColor("#478EF8")
                }
                
            }else{
                timeLabel2.text = String(format: "MT%d ( %@ )", self.number! ,str2)
                
                topLabel.layer.borderColor = UIColor("#00A291")?.cgColor
                //topLabel.backgroundColor = UIColor("#00A291")
                if myModel?.can_finish == "0"{
                   topLabel.layer.borderColor = UIColor("#AB46BB")?.cgColor
                    //topLabel.backgroundColor = UIColor("#F85858")
                }
            }
            
            
             if (mytempDic as! Dictionary<String, Any>)["type"] as! Int == 2 {
                topLabel.layer.borderColor = UIColor("#26DBCC")?.cgColor
                grayBackView.backgroundColor = UIColor.init(cgColor: topLabel.layer.borderColor!)

            }
            
            topLabel.backgroundColor = UIColor.clear
            grayBackView.backgroundColor = UIColor.init(cgColor: topLabel.layer.borderColor!) 
            
            
            timeLabel2.font = UIFont.systemFont(ofSize: 12)
        }
        get{
            return (self.myDic)
        }
    }
    
    
    var model :productModel{
        set {
            myModel = newValue;
            let str2 = myModel?.shengyu?.replacingOccurrences(of: " ", with: "天")
            leftLabel.text = String(format: "已产出 %@ DKE", (myModel?.d_dke!)!)
            leftLabel.adjustsFontSizeToFitWidth = true
            
            rightLabel.text = String(format: "已保值 %@M 还可保值 %@M", (myModel?.baozhi_m!)!,(myModel?.can_m!)!)
            rightLabel.adjustsFontSizeToFitWidth = true
            if self.typeNumber == 0{
                timeLabel2.text = String(format: "CD%d ( %@ )", self.number! ,str2!)
                if myModel?.baozhi_status == "0"{
                    //topLabel.backgroundColor = UIColor("#F952D3")
                    
                    topLabel.layer.borderColor = UIColor("#F952D3")?.cgColor
                }else{
                   topLabel.layer.borderColor = UIColor("#478EF8")?.cgColor
//                    topLabel.backgroundColor = UIColor("#478EF8")
                }
                
                if myModel?.all_baozhi == "1"{
                   
                   topLabel.layer.borderColor = UIColor("#478EF8")?.cgColor
                    //topLabel.backgroundColor = UIColor("#F9AB5D")
                }
                
                
            }else{
                timeLabel2.text = String(format: "MT%d ( %@ )", self.number! ,str2!)
               topLabel.layer.borderColor = UIColor("#00A291")?.cgColor
                //topLabel.backgroundColor = UIColor("#00A291")
                if myModel?.can_finish == "0"{
                   topLabel.layer.borderColor = UIColor("#AB46BB")?.cgColor
                    //topLabel.backgroundColor = UIColor("#F85858")
                }
            }
            
            
            
            timeLabel2.font = UIFont.systemFont(ofSize: 12)
        }
        get
        {
            return (self.myModel)!
            
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier)
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.countDownNotification), name: NSNotification.Name(rawValue: "OYCountDownNotification"), object: nil)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.countDownNotification), name: NSNotification.Name(rawValue: "OYCountDownNotification"), object: nil)
    }
    
    @objc private func countDownNotification() {
        // 计算倒计时 count
        if self.mytempDic.count == 0 {
            
        }else{
            let countDown =
                (mytempDic as! Dictionary<String, Any>)["shengyu_time"] as! Int - OYCountDownManager.sharedManager.timeInterval
            
            if countDown <= 0 {
                // 倒计时结束时回调
                return;
            }
            
            // 重新赋值
            let str2 = String(format: "%02d%@%02d:%02d:%02d",(countDown/(3600 * 24)) ,XALanguageManager.getString(key: "天"),(countDown/3600)%24, (countDown/60)%60, countDown%60)
            if self.isfirst == 0 {
                if mnType == "2"{
                    timeLabel2.text = String(format: "MT%d  ( %@ )", self.number! ,str2)
                    if (mytempDic as! Dictionary<String, Any>)["status"] as! Int == 1 {
                        timeLabel2.text = String(format: "MT%d  ( 0%@%@ )", self.number! ,XALanguageManager.getString(key: "天"),"00:00:00")
                    }
                    
                    
                }else{
                    timeLabel2.text = String(format: "CD%d  ( %@ )", self.number! ,str2)
                    if (mytempDic as! Dictionary<String, Any>)["status"] as! Int == 1 {
                        timeLabel2.text = String(format: "CD%d  ( 0%@%@ )", self.number! ,XALanguageManager.getString(key: "天"),"00:00:00")
                    }
                }
            }else{
                if mnType == "2"{
                    timeLabel2.text = String(format: "MT%d  · %@  ( %@ )", self.number! ,(mytempDic as! Dictionary<String, Any>)["n"] as! String,str2)
                    if (mytempDic as! Dictionary<String, Any>)["status"] as! Int == 1 {
                        timeLabel2.text = String(format: "MT%d  ( 0%@%@ )", self.number! ,XALanguageManager.getString(key: "天"),"00:00:00")
                    }
                }else{
                    timeLabel2.text = String(format: "CD%d  · %@  ( %@ )", self.number! ,(mytempDic as! Dictionary<String, Any>)["m"] as! String,str2)
                    if (mytempDic as! Dictionary<String, Any>)["status"] as! Int == 1 {
                        timeLabel2.text = String(format: "CD%d  ( 0%@%@ )", self.number!,XALanguageManager.getString(key: "天") ,"00:00:00")
                    }
                }
                
                
                
            }
            
            
            //self.detailTextLabel?.text =
            //print(self.detailTextLabel?.text)
        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        topLabel.layer.borderColor = UIColor("#418DFF")?.cgColor
        //topLabel.backgroundColor = UIColor("#418DFF")
        
        //#F9AB5D  //#F951D0
        topLabel.layer.cornerRadius = 10
        topLabel.clipsToBounds = true
        self.backgroundColor = UIColor("#1F2647")
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
